<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Amitav Roy
 * Date: 1/26/13
 * Time: 10:41 PM
 * To change this template use File | Settings | File Templates.
 */

// defining the image source url
define('image_path','http://cms.amitavroy.com');

// dsm function
if (!function_exists('dsm'))
{
  function dsm($var, $exit = 0) {
    print '<div id="spit"><pre>';
    print_r($var);
    print '</pre></div>';
		if ($exit == 1) {
			exit();
		}
  }
}

// dsm with die function
if (!function_exists('dd'))
{
  function dd($var) {
    var_dump($var);
    die();
  }
}

// load image from uri
if (!function_exists('load_image'))
{
  function load_image($style,$uri,$attr = '') {
    $uri = substr($uri, 9);
    $domain = image_path;
    $folder_path = '/sites/default/files/styles/'.$style;
    $access = '/public/'.$uri;

    return '<img src="'.
      $domain.
      $folder_path.
      $access.'" '.$attr.' />';
  }
}

// give image url
if (!function_exists('get_image_url'))
{
  function get_image_url($style,$uri,$attr = '') {
    $uri = substr($uri, 9);
    $domain = image_path;
    $folder_path = '/sites/default/files/styles/'.$style;
    $access = '/public/'.$uri;
    return $domain . $folder_path . $access;
  }
}

if (!function_exists('php_formatter'))
{
  function php_formatter($data) {
    return eval('?>' . $data);
  }
}

// get_image function
if (!function_exists('get_image'))
{
  function get_image($id) {
    $table_name = 'file_managed';
    $path = 'http://cms.amitavroy.com/sites/default/files/styles/screen_grab/public/';
    $ci =& get_instance(); // getting the CI instance
    $ci->db->select('fm.uri, alt.field_file_image_alt_text_value');
    $ci->db->from($table_name . ' as fm');

    $ci->db->join('field_revision_field_file_image_alt_text alt', 'alt.entity_id=fm.fid');

    $ci->db->where('fm.fid', $id);
    $ci->db->where('fm.status', 1);
    $ci->db->where('fm.type', 'image');
    $query = $ci->db->get();
    if ($query->num_rows() > 0) {
      $query = $query->result_array();
      $uri = $query[0]['uri'];
      $uri = substr($uri, 9);
      return '<div class="get-image-container"><img src="'.$path . $uri.'" class="get-image"
        alt="'.$query[0]['field_file_image_alt_text_value'].'"
        title="'.$query[0]['field_file_image_alt_text_value'].'" />
        <p>'.$query[0]['field_file_image_alt_text_value'].'</p></div>';
    }
    else {
      return false;
    }
  }
}

if (!function_exists('is_ajax_req'))
{
  function is_ajax_req() {
    if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']=="XMLHttpRequest")
      return true;
    else
      return redirect(base_url());
  }
}

/**
 * These functions are to setup the og meta data
 * to share the articles better on SNS sites like Facebook and Google+
 */
if (!function_exists('get_og_url'))
{
  function get_og_url($alias) {
    $basic_url = 'http://amitavroy.com/';
    $final_url = $basic_url . $alias;
    return $final_url;
  }
}
if (!function_exists('get_og_image'))
{
  function get_og_image($nid) {
    $ci =& get_instance(); // getting the CI instance

    $ci->db->select('fm.uri');
    $ci->db->from('field_data_field_preview_image fpi');
    $ci->db->where('fpi.entity_type','node');
    $ci->db->where('fpi.entity_id',$nid);
    $ci->db->join('file_managed fm', 'fm.fid=fpi.field_preview_image_fid');
    $query = $ci->db->get();
    $result = $query->row_array();
//    dd($ci->db->last_query());
   // dd($result);
    $url = image_path;
    $folder_path = '/sites/default/files/preview_images/';
    $image_uri_arr = explode('/',$result['uri']);

    $array_count = count($image_uri_arr) - 1; // always the last item in the array
    $final_path = $url . $folder_path . $image_uri_arr[$array_count];
    return $final_path;
  }
}

if (!function_exists('auth_user'))
{
  function auth_user() {
    $ci =& get_instance(); // getting the CI instance
    $ci->load->library('session');
    $email = $ci->session->userdata('email');
    if (!$email) {
      redirect('/');
    }
  }
}

/**
 * This function will take a string and trim it to a limit base on the second parameter limit.
 * This will remove the last incomplete word also.
 */
if (!function_exists('trim_text'))
{
  function trim_text($string, $limit) {
    $bound = $limit;
    // if string length less than 70 then return without modification
    if (strlen($string) <= $bound) {
      return $string;
    }

    $string_in_bounds = substr($string, 0, $bound); // getting the string inside allowed char limit
    $string_array = explode(" ", $string_in_bounds); // exploding to an array and getting rid of last word.
    $number_words = count($string_array) - 1;

    $final_string = '';
    foreach ($string_array as $key => $word) {
      if ($key != $number_words) {
        $final_string .= $word . ' ';
      }
    }

    $final_string = substr($final_string, 0, -1); // removing the trailing space.

    return $final_string;
  }
}

if (!function_exists('trim_text_with_dots'))
{
  function trim_text_with_dots($string, $limit) {
    $string = trim_text($string, $limit);
    return $string . '&#8230;';
  }
}